﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Orchard;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Aspects;
using Orchard.ContentManagement.Handlers;
using Orchard.Messaging.Services;
using Orchard.Mvc.Filters;
using Orchard.Mvc.Html;
using Orchard.Utility.Extensions;

namespace Contrib.CommentNotifications.Handlers
{
    public class CommentPartHandler : ContentHandler {
        private readonly IOrchardServices _orchardServices;
        public const string CommentCreated = "COMMENT_CREATED";

        public CommentPartHandler(IOrchardServices orchardServices) {
            _orchardServices = orchardServices;
        }

        protected override void Published(PublishContentContext context) {
            if(context.ContentType != "Comment") {
                return;
            }

            var commented = new Commented { Comment = context.ContentItem };
            _orchardServices.WorkContext.HttpContext.Items.Add(Guid.NewGuid().ToString(), commented);
        }
    }

    public class Commented {
        public ContentItem Comment { get; set; }
    }

    public class AdminNotificationFilter : FilterProvider, IActionFilter {
        private readonly IMessageManager _messageManager;
        private readonly IOrchardServices _orchardServices;

        public AdminNotificationFilter(IMessageManager messageManager, IOrchardServices orchardServices) {
            _messageManager = messageManager;
            _orchardServices = orchardServices;
        }

        public void OnActionExecuted(ActionExecutedContext filterContext) {
            if(filterContext.ActionDescriptor.ActionName == "Create"
                && filterContext.ActionDescriptor.ControllerDescriptor.ControllerName == "Comment") {
                foreach(var commented in filterContext.HttpContext.Items.Values.OfType<Commented>()) {

                    var comment = (dynamic) commented.Comment;
                    int contentItemId = comment.CommentPart.Record.CommentedOn;

                    var container = _orchardServices.ContentManager.Get(contentItemId);

                    if (container == null) {
                        return;
                    }

                    var common = container.As<ICommonPart>();

                    if (common == null) {
                        return;
                    }

                    var title = "";
                    if (((dynamic)container).RoutePart != null) {
                        title = (string)((dynamic)container).RoutePart.Title;
                    }

                    var urlHelper = new UrlHelper(filterContext.HttpContext.Request.RequestContext);
                    var siteUrl = urlHelper.RequestContext.HttpContext.Request.ToRootUrlString();
                    var displayUrl = siteUrl + urlHelper.ItemDisplayUrl(container);

                    var arguments = new Dictionary<string, string> {
                                                { "Author", (string)comment.CommentPart.Record.Author },
                                                { "SiteName", (string)comment.CommentPart.Record.SiteName },
                                                { "UserName", (string)comment.CommentPart.Record.UserName },
                                                { "Email", (string)comment.CommentPart.Record.Email },
                                                { "Status", comment.CommentPart.Record.Status.ToString() },
                                                { "CommentText", (string)comment.CommentPart.Record.CommentText },
                                                { "Title", title },
                                                { "DisplayUrl",  displayUrl}
                                            };

                    _messageManager.Send(common.Owner.ContentItem.Record, CommentPartHandler.CommentCreated, "email", arguments);
                }
            }
        }

        public void OnActionExecuting(ActionExecutingContext filterContext) {
        }
    }
}